using Godot;
using System;

public partial class 玩法弹窗继承 : Control
{
    [Export] private Label 弹窗标题;
    [Export] private Button 关闭按钮;
    [Export] private ColorRect 弹窗遮罩;
    [Export] private VBoxContainer 按钮容器;
    [Export] public TabContainer 选项卡;

    private Godot.Collections.Array<Node> 按钮组;

    public override void _Ready()
    {
        关闭按钮.Pressed += QueueFree;
        弹窗遮罩.GuiInput += (@event =>
        {
            if (@event is InputEventMouseButton mouseEvent)
            {
                if (mouseEvent.ButtonIndex == MouseButton.Left && mouseEvent.Pressed)
                {
                    QueueFree();
                }
            }
        });

        按钮组 = 按钮容器.GetChildren();
        for (int i = 0; i < 按钮组.Count; i++)
        {
            Button 按钮 = (Button)按钮组[i];
            int 索引 = i;
            if (i == 0)
            {
                改变按钮样式(按钮);
            }
            按钮.Pressed += () =>
            {
                切换选项卡时(索引);
            };
        }
        选项卡.TabChanged += 切换选项卡时;
        切换选项卡时(0);
    }

    private void 置窗口标题(string 标题)
    {
        弹窗标题.Text = 标题;
    }

    private void 改变按钮样式(Button 按钮, string 颜色 = "yellow")
    {
        按钮.AddThemeColorOverride("font_color", new Color(颜色));
        按钮.AddThemeColorOverride("font_hover_color", new Color(颜色));
        按钮.AddThemeColorOverride("font_focus_color", new Color(颜色));
        按钮.AddThemeColorOverride("font_pressed_color", new Color(颜色));
    }

    private void 切换选项卡时(long 选项卡索引)
    {
        Button 当前按钮 = (Button)按钮组[选项卡.CurrentTab];
        Button 待选按钮 = (Button)按钮组[(int)选项卡索引];
        if (当前按钮 != null && 待选按钮 != null)
        {
            改变按钮样式(当前按钮, "#ffffff");
            改变按钮样式(待选按钮);
            置窗口标题(待选按钮.Text);
            选项卡.CurrentTab = (int)选项卡索引;
            var 功能实例 = 选项卡.GetTabControl(选项卡.CurrentTab).GetChild(0);
            // 判断Node节点中是否包含某个方法 如果包含就执行
            if (功能实例 != null && 功能实例.HasMethod("初始化"))
            {
                功能实例.Call("初始化");
            }
        }
    }
}
